package com.bw.lyc.mvp.view.mine;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.bw.lyc.R;
import com.bw.lyc.mvp.base.model.BaseEntity;
import com.bw.lyc.mvp.base.view.BaseActivity;
import com.bw.lyc.mvp.contract.LoginContract;
import com.bw.lyc.mvp.di.DaggerLoginComponent;
import com.bw.lyc.mvp.di.LoginModules;
import com.bw.lyc.mvp.model.HttpCode;
import com.bw.lyc.mvp.model.db.DBManager;
import com.bw.lyc.mvp.model.entity.ResponseUserEntity;
import com.bw.lyc.mvp.model.entity.UserEntity;
import com.bw.lyc.mvp.presenter.LoginPresenter;
import com.bw.lyc.mvp.view.FrameActivity;
import com.bw.lyc.util.ToastUtils;

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.ILoginView, View.OnClickListener {

    private LinearLayout loginLl;
    private EditText loginPhone;
    private EditText loginCode;
    private LinearLayout registerLl;
    private EditText registerPhone;
    private EditText registerPassword;
    private Button registerBt;
    private TextView registerTv;
    private ImageView registerPasswordShow;
    private boolean flag = true;
    private ImageView viewTopBack;
    private Button loginBt;
    private TextView loginTv;
    private ResponseUserEntity entity1;
    private UserEntity values;

    @Override
    public View addStatusView() {
        return f(R.id.login_linear);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        setStatusBarColor(Color.WHITE);


        loginBt = (Button) findViewById(R.id.login_bt);
        loginTv = (TextView) findViewById(R.id.login_tv);
        viewTopBack = (ImageView) findViewById(R.id.view_top_back);
        loginLl = (LinearLayout) findViewById(R.id.login_ll);
        loginPhone = (EditText) findViewById(R.id.login_phone);
        loginCode = (EditText) findViewById(R.id.login_code);
        registerLl = (LinearLayout) findViewById(R.id.register_ll);
        registerPhone = (EditText) findViewById(R.id.register_phone);
        registerPassword = (EditText) findViewById(R.id.register_password);
        registerBt = (Button) findViewById(R.id.register_bt);
        registerTv = (TextView) findViewById(R.id.register_tv);
        registerPasswordShow = (ImageView) findViewById(R.id.register_password_show);

        registerPasswordShow.setOnClickListener(this::onClick);
        registerBt.setOnClickListener(this::onClick);
        viewTopBack.setOnClickListener(this::onClick);
        loginBt.setOnClickListener(this::onClick);
        loginTv.setOnClickListener(this::onClick);
        registerTv.setOnClickListener(this::onClick);


    }

    @Override
    public void inject() {
        DaggerLoginComponent.builder().loginModules(new LoginModules(this)).build().injectLogin(this);
        DaggerLoginComponent.builder().loginModules(new LoginModules(this)).build().injectRegister(this);
    }

    @Override
    public void initData() {


    }

    @Override
    public void refreshView(BaseEntity entity) {
        entity1 = (ResponseUserEntity) entity;
        values = entity1.getValues();
        if (entity1.status.equals("200")) {
            int user_id = values.getUser_id();
            String user_img = values.getUser_img();
            int user_integral = values.getUser_integral();
            String user_name = values.getUser_name();
            int user_phone = values.getUser_phone();
            String user_pwd = values.getUser_pwd();
            String user_sex = values.getUser_sex();
            try {
                DBManager.getInstance().getDaoSession().insert(new UserEntity(user_id, user_phone, user_pwd, user_name, user_sex, user_img, user_integral));
                startPage(UpdateUserActivity.class);
                finish();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                LogUtils.json(
                        DBManager.getInstance().getDaoSession().loadAll(UserEntity.class)
                );

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.view_top_back) {
            ToastUtils.showMsg("登录失败，返回主页面");
            startPage(FrameActivity.class);
            finish();
        }
        if (id == R.id.register_bt) {
            String phone = registerPhone.getText().toString().trim();
            String password = registerPassword.getText().toString().trim();

            if (phone.length() == 0 && password.length() == 0) {
                ToastUtils.showMsg("手机号和密码不能为空");
            } else if (phone.length() >= 10) {
                ToastUtils.showMsg("手机号需10以内");
            } else {
                p.requestLogin(Long.valueOf(phone), password,
                        HttpCode.REGISTCODE);
            }
        }
        if (id == R.id.register_password_show) {
            if (flag) {
                registerPasswordShow.setImageResource(R.mipmap.res_7icon_202);
                registerPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//隐藏密码
                flag = false;
            } else {
                registerPasswordShow.setImageResource(R.mipmap.res_7icon_201);
                registerPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示密码
                flag = true;
            }
        }
        if (id == R.id.login_bt) {
            String phone = loginPhone.getText().toString().trim();
            String password = loginCode.getText().toString().trim();
            if (phone.length() == 0 && password.length() == 0) {
                ToastUtils.showMsg("手机号和密码不能为空");
            } else if (phone.length() >= 10) {
                ToastUtils.showMsg("手机号输入错误");
            } else {
                p.requestLogin(Long.valueOf(phone), password,
                        HttpCode.LOGINCODE);
            }
        }
        if (id == R.id.register_tv) {
            AnimatorSet animatorSet = new AnimatorSet();
            ObjectAnimator translationY = ObjectAnimator.ofFloat(registerLl, "translationY", 0, 200);
            ObjectAnimator alpha = ObjectAnimator.ofFloat(registerLl, "alpha", 1, 0);
            animatorSet.setInterpolator(new LinearInterpolator());
            animatorSet.setDuration(1000);
            animatorSet.playTogether(translationY, alpha);
            animatorSet.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {

                    registerLl.setVisibility(View.GONE);
                    loginLl.setVisibility(View.VISIBLE);
                    registerPhone.setText("");
                    registerPassword.setText("");

                    AnimatorSet animatorSet1 = new AnimatorSet();
                    ObjectAnimator alpha1 = ObjectAnimator.ofFloat(registerLl, "alpha", 0, 1);
                    ObjectAnimator translationY1 = ObjectAnimator.ofFloat(registerLl, "translationY", 200, 0);
                    animatorSet1.setInterpolator(new LinearInterpolator());
                    animatorSet1.setDuration(1000);
                    animatorSet1.playTogether(translationY1, alpha1);
                    animatorSet1.start();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            animatorSet.start();
        }
        if (id == R.id.login_tv) {
            AnimatorSet animatorSet = new AnimatorSet();
            ObjectAnimator translationY = ObjectAnimator.ofFloat(loginLl, "translationY", 0, 200);
            ObjectAnimator alpha = ObjectAnimator.ofFloat(loginLl, "alpha", 1, 0);
            animatorSet.setInterpolator(new LinearInterpolator());
            animatorSet.setDuration(1000);
            animatorSet.playTogether(translationY, alpha);
            animatorSet.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {

                    loginLl.setVisibility(View.GONE);
                    registerLl.setVisibility(View.VISIBLE);
                    loginPhone.setText("");
                    loginCode.setText("");

                    AnimatorSet animatorSet1 = new AnimatorSet();
                    ObjectAnimator alpha1 = ObjectAnimator.ofFloat(loginLl, "alpha", 0, 1);
                    ObjectAnimator translationY1 = ObjectAnimator.ofFloat(loginLl, "translationY", 200, 0);
                    animatorSet1.setInterpolator(new LinearInterpolator());
                    animatorSet1.setDuration(1000);
                    animatorSet1.playTogether(translationY1, alpha1);
                    animatorSet1.start();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            animatorSet.start();
        }
    }
}